/**
 * 1
 *
 * @Author : roryrywang
 * @DateTime : 24-9-4 下午6:54
 */

#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

int getLevel(int x) {
    int res = 0;
    while (x & 1) {
        res++;
        x >>= 1;
    }
    return res;
}

bool cmp(pair<int, int> p1, pair<int, int> p2) {
    return p1.second == p2.second ? p1.first > p2.first : p1.second > p2.second;
}

int main() {
    int n1;
    cin >> n1;
    vector<int> node1(n1 + 1);
    for (int i = 1; i <= n1; ++i) {
        cin >> node1[i];
    }
    int n2;
    cin >> n2;
    vector<int> node2(n2 + 1);
    for (int i = 1; i <= n2; ++i) {
        cin >> node2[i];
    }
    int k1 = getLevel(n1);
    int k2 = getLevel(n1);
    int k = min(k1, k2);
    int l = 0, r = 0;
    unordered_map<int, int> map;
    for (int i = 1; i <= k; ++i) {
        l = r + 1;
        r = 2 * r + 1;
        unordered_map<int, int> tmp;
        for (int j = l; j <= r; ++j) {
            tmp[node1[j]]++;
        }
        for (int j = l; j <= r; ++j) {
            if (tmp.count(node2[j])) tmp[node2[j]]--;
        }
        for (auto p: tmp) {
            map[p.first] += p.second;
        }
    }
    for (int i = k + 1; i <= k1; ++i) {
        l = r + 1;
        r = 2 * r + 1;
        for (int j = l; j <= r; ++j) {
            map[node1[j]]++;
        }
    }

    vector<pair<int, int>> res(map.begin(), map.end());

    sort(res.begin(), res.end(), cmp);

    string resString;
    for (auto u: res) {
        if (u.second != 0)resString += to_string(u.first);
    }

    if (!resString.empty()) {
        cout << resString;
    } else {
        cout << "0";
    }

    return 0;
}